package com.beauty.dressing.module.community;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.beauty.dressing.R;
import com.beauty.dressing.common.base.BaseFragment;
import com.beauty.dressing.common.bean.Ad;
import com.beauty.dressing.common.bean.BaseBean;
import com.beauty.dressing.common.bean.Post;
import com.beauty.dressing.common.bean.TopicTitle;
import com.beauty.dressing.common.utils.ResUtil;
import com.beauty.dressing.logic.control.LBController;
import com.beauty.dressing.module.common.view.BannerLayout;
import com.beauty.dressing.module.community.adapter.CommunityAdapter;
import com.beauty.dressing.module.community.holder.CommunityHeaderHolder;
import com.beauty.dressing.module.main.activity.PostSearchActivity;
import com.beauty.dressing.module.main.adapter.CourseListAdapter;
import com.framework.library.haorefresh.HaoRecyclerView;
import com.framework.library.haorefresh.ProgressView;
import com.xiaolanba.commonlib.fresco.FrescoUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 论坛
 * @author xutingz
 * @E-mail xutz@xianlanba.com
 * @date 2018/05/02
 */

public class CommunityFragment extends BaseFragment {
    private HaoRecyclerView mRecyclerView;
    private CommunityAdapter mAdapter;
    private BannerLayout mBannerHeader;
    private CommunityHeaderHolder mHeaderHolder;
    private ImageView mSearch;
    SwipeRefreshLayout mSwipeRefresh;
    @Override
    public void setContentView() {
        setContentView(R.layout.fragment_community);
    }

    @Override
    public void findView() {
        mRecyclerView = (HaoRecyclerView) findViewById(R.id.recyclerview);
        mBannerHeader = new BannerLayout(context);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.addOnScrollListener(FrescoUtil.getPauseOnScrollListener());
        mSearch = (ImageView) findViewById(R.id.search_img);
        mSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PostSearchActivity.startActivity(context,null);
            }
        });

        mSwipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);
        mSwipeRefresh.setColorSchemeResources(R.color.theme_blue, R.color.theme_blue, R.color.theme_blue);
        //设置自定义加载中和到底了效果
        ProgressView progressView = new ProgressView(context);
        progressView.setIndicatorId(ProgressView.BallPulse);
        progressView.setIndicatorColor(ResUtil.getColor(R.color.theme_blue));
        mRecyclerView.setFootLoadingView(progressView);
        TextView textView = new TextView(context);
        textView.setText("全部加载完成");
        mRecyclerView.setFootEndView(textView);
        mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                LBController.MainHandler.postDelayed(new Runnable() {
                    public void run() {
                        mSwipeRefresh.setRefreshing(false);

                    }
                }, 2000);

            }
        });
        mRecyclerView.loadMoreEnd();

        findViewById(R.id.search_txt1).setOnClickListener(this);
        findViewById(R.id.search_txt2).setOnClickListener(this);
        findViewById(R.id.search_txt3).setOnClickListener(this);
        findViewById(R.id.search_txt4).setOnClickListener(this);
        findViewById(R.id.search_txt5).setOnClickListener(this);
    }

    @Override
    public void initData() {
        mAdapter = new CommunityAdapter(context);
        mHeaderHolder = new CommunityHeaderHolder(context,mInflater,mRecyclerView);
        mAdapter.addHeaderView(mHeaderHolder.itemView);
        mHeaderHolder.bindData(null,0);
        List<BaseBean> topicTitles = new ArrayList<>();
        for (int i=0;i<4;i++){
            topicTitles.add(new TopicTitle());
        }
        mAdapter.setTopList(topicTitles);
        List<BaseBean> list = new ArrayList<>();
        for (int i=0;i<10;i++){
            list.add(new Post());
        }
        mAdapter.setPostList(list);

        mRecyclerView.setAdapter(mAdapter);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.search_txt1:
            case R.id.search_txt2:
            case R.id.search_txt3:
            case R.id.search_txt4:
            case R.id.search_txt5:
                PostSearchActivity.startActivity(context,((TextView)v).getText().toString());
                break;
        }
    }

    public void tabReFocus(){
        if (mRecyclerView != null){
            mRecyclerView.getLayoutManager().scrollToPosition(0);
        }
    }
}
